package br.ufrn.dca.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

/**
 * 
 * 
 * @author Rafael Holanda
 *
 */
public class TelaConfiguracao extends JFrame {

	private JPanel contentPane;
	private JTextField enderecoField;
	private JTextField portaField;
	private TelaPrincipal principal;

	/**
	 * Create the frame.
	 */
	public TelaConfiguracao(TelaPrincipal principal) {
		this.principal = principal;
		
		setTitle("Configura\u00E7\u00E3o - TANKER CONTROL");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 292, 185);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
//		setResizable(false);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(TelaConfiguracao.class.getResource("/br/ufrn/dca/gui/icon.jpg")));
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnCancelarActionPerformed(arg0);
			}
		});
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnOkActionPerformed(e);
			}
		});
		
		portaField = new JTextField(String.valueOf(principal.getPorta()));
		portaField.setColumns(10);
		
		enderecoField = new JTextField(principal.getEndereco());
		enderecoField.setColumns(10);
		
		JLabel lblEndereoIp = new JLabel("Endere\u00E7o IP:");
		
		JLabel lblPorta = new JLabel("Porta:");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(128)
					.addComponent(btnOk)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnCancelar)
					.addContainerGap())
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addComponent(label)
					.addContainerGap(236, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(43, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(32)
							.addComponent(lblPorta)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(portaField, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblEndereoIp)
							.addGap(4)
							.addComponent(enderecoField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))
					.addGap(36))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(47, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(lblPorta))
						.addComponent(portaField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(9)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(lblEndereoIp))
						.addComponent(enderecoField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCancelar)
						.addComponent(btnOk)))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(label)
					.addContainerGap(107, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}

	private void btnOkActionPerformed(ActionEvent e) {
		principal.configurar(enderecoField.getText(), Integer.parseInt(portaField.getText()));
		this.dispose();
	}

	private void btnCancelarActionPerformed(ActionEvent evt) {
		this.dispose();
	}
}
